package com.study.flink.java.day08_tableAPI;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.FlatMapIterator;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.java.StreamTableEnvironment;
import org.apache.flink.util.Collector;

import java.util.Arrays;

/**
 * Flink Table API示例  流处理
 */
public class StreamSqlWordCount {

    public static void main(String[] args) throws Exception {

        // 实时DataStreamAPI
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);
        // 创建一个实时Table执行上下文
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        // word count spark hadoop flink
        DataStreamSource<String> lines = env.socketTextStream("node02", 8888);

        // 转化为结构化数据
        SingleOutputStreamOperator<String> words = lines.flatMap(new FlatMapFunction<String, String>() {
            @Override
            public void flatMap(String line, Collector<String> out) throws Exception {
                Arrays.stream(line.split(" ")).forEach(out::collect);
            }
        });

        // 注册表
        tableEnv.registerDataStream("t_wordcount", words, "word");
        // 写SQL
        Table table = tableEnv.sqlQuery("select word, count(1) counts from t_wordcount group by word");
        // 聚合打印
        DataStream<Tuple2<Boolean, WordCountEntity>> dataStream = tableEnv.toRetractStream(table, WordCountEntity.class);
        SingleOutputStreamOperator<Tuple2<Boolean, WordCountEntity>> filterDataStream = dataStream.filter(new FilterFunction<Tuple2<Boolean, WordCountEntity>>() {
            @Override
            public boolean filter(Tuple2<Boolean, WordCountEntity> tp) throws Exception {
                return tp.f0;
            }
        });
        filterDataStream.print();

        env.execute("StreamSqlWordCount");

    }


}
